package cn.lazyturtle.bugpush.dao.user;

import cn.lazyturtle.bugpush.jooq.tables.daos.BUserDao;
import cn.lazyturtle.bugpush.jooq.tables.pojos.BUser;
import org.jooq.DSLContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Map;

import static cn.lazyturtle.bugpush.jooq.tables.BUser.b_user;
/**
 * <p>
 * UserDao :
 * </p>
 *
 * @author cwh
 * @since 2021-11-02
 */
@Repository
public class UserDao extends BUserDao {
    private final DSLContext dslContext;

    @Autowired
    public UserDao(DSLContext dslContext) {
        this.dslContext = dslContext;
        setConfiguration(dslContext.configuration());
    }

    public BUser selectUserByDingDingId(String dingDingId){
        return dslContext.select()
            .from(b_user)
            .where(b_user.ding_id.eq(dingDingId),
                b_user.is_delete.eq((byte)0))
            .fetchAnyInto(BUser.class);
    }

    public Map<String, String> selectUser(){
        return dslContext.select()
            .from(b_user)
            .where(b_user.is_delete.eq((byte)0))
            .fetchMap(b_user.pinyin_name,b_user.ding_id);
    }

    public void deleteUserByDingDingId(String dingDingId) {
        dslContext.update(b_user)
            .set(b_user.is_delete,(byte)1)
            .where(b_user.ding_id.eq(dingDingId), b_user.is_delete.eq((byte)0)).execute();
    }
}
